//
//  BKPopView.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/24.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "BKPopView.h"
#import <ReactiveCocoa.h>
#import <UIButton+RACCommandSupport.h>
#import <RACEXTScope.h>

@interface BKPopView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,weak) UIView *parentView;
@property (nonatomic ,strong) UIView *maskView;
@property (nonatomic ,strong) UITableView *tableView;

@end
@implementation BKPopView



- (id)initWithView:(UIView *)view{

    self = [super initWithFrame:CGRectMake(view.frame.size.width/2-100, view.frame.size.height/2-150, 200, 300)];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.976 green:0.969 blue:0.914 alpha:1.000];
        _parentView = view;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.frame.size.width, self.frame.size.height-80)];
        _tableView.backgroundColor= [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        
        [self addSubview:_tableView];
        UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(20, self.frame.size.height-50, self.frame.size.width/2-30, 34)];
        [confirmButton setBackgroundColor:[UIColor colorWithRed:0.992 green:0.686 blue:0.369 alpha:1.000]];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
        confirmButton.layer.cornerRadius = 1;
        [self addSubview:confirmButton];
        @weakify(self);
        [[confirmButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            NSIndexPath *path = [self.tableView indexPathForSelectedRow];
            [self.delegate bkPopView:self confirmItem:self.dataArray[path.row]];
            [self p_hide];
        }];
        
        
        UIButton *cancleButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2+10, self.frame.size.height-50, self.frame.size.width/2-30, 34)];
        [cancleButton setBackgroundColor:[UIColor colorWithRed:0.992 green:0.686 blue:0.369 alpha:1.000]];
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        cancleButton.titleLabel.font = [UIFont systemFontOfSize:16];
        cancleButton.layer.cornerRadius = 1;
        [self addSubview:cancleButton];
        
        [[cancleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self p_hide];
        }];

    }
    return self;
}

- (void)show{
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _parentView.frame.size.width, _parentView.frame.size.height)];
    
    _maskView.alpha = .6;
    _maskView.backgroundColor = [UIColor blackColor];
    [_parentView addSubview:_maskView];
    [_parentView addSubview:self];
    [_parentView bringSubviewToFront:self];
    
    
}


- (void)p_hide{
    [_maskView removeFromSuperview];
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *string = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
       [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:string];
       cell = [tableView dequeueReusableCellWithIdentifier:string];
    }
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = _dataArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_dataArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 20)];
    return footerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 20;
}
@end
